//package com.xiguaji.tiktok.task.awemecomment.event;
//
//import com.google.common.eventbus.AllowConcurrentEvents;
//import com.google.common.eventbus.Subscribe;
//import com.gugee.tiktok.common.model.dal.blogger.BloggerLogRepository;
//import com.gugee.tiktok.common.model.dal.blogger.BloggerMainRepository;
//import com.gugee.tiktok.common.model.dto.SlimBloggerDto;
//import com.gugee.tiktok.common.model.mysql.BloggerLog;
//import com.xiguaji.boot.eventbus.Event;
//import com.xiguaji.boot.eventbus.EventBusTemplate;
//import com.xiguaji.boot.eventbus.IEventListener;
//import com.xiguaji.boot.toolkit.ThreadPoolUtils;
//import com.xiguaji.tiktok.task.aweme.AwemeService;
//import com.xiguaji.tiktok.task.aweme.event.RefreshAwemeCategoriesEventListener;
//import com.xiguaji.tiktok.task.blogger.event.UpdateBloggerMainEventListener;
//import com.xiguaji.tiktok.task.monitor.LogUtil;
//import com.xiguaji.tiktok.task.monitor.Monitor;
//import com.xxl.job.core.log.XxlJobLogger;
//import lombok.SneakyThrows;
//import lombok.Synchronized;
//import lombok.extern.slf4j.Slf4j;
//import lombok.val;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//
//import java.util.List;
//import java.util.Optional;
//import java.util.concurrent.ExecutionException;
//import java.util.concurrent.ForkJoinPool;
//import java.util.concurrent.atomic.AtomicInteger;
//import java.util.stream.Collectors;
//
//@Component
//@Slf4j
//public class RefreshAwemeCommentEventListener implements IEventListener<RefreshAwemeCommentEventListener.RefreshAwemeCommentEvent> {
//    @Autowired
//    BloggerLogRepository bloggerLogRepository;
//
//    @Autowired
//    BloggerMainRepository bloggerMainRepository;
//
//    @Autowired
//    @Lazy
//    EventBusTemplate eventBusTemplate;
//
//    @Autowired
//    AwemeService awemeService;
//
//    @SneakyThrows
//    @Override
//    @Subscribe
//    @AllowConcurrentEvents
//    public void handle(RefreshAwemeCommentEvent event) {
//        run(event);
//    }
//
//    public void run(RefreshAwemeCommentEvent event) throws ExecutionException, InterruptedException {
//        int bloggerLogType = BloggerLog.BloggerLogType.FETCH_AWEME_COMMENT_LOOP.getValue();
//
//        Optional<BloggerLog> bloggerLogOptional = bloggerLogRepository.findByLogType(bloggerLogType);
//        BloggerLog bloggerLog;
//
//        int page = 1;
//        if (bloggerLogOptional.isPresent()) {
//            page = (int)bloggerLogOptional.get().getCurrentBloggerId();
//            bloggerLog = bloggerLogOptional.get();
//        } else {
//            bloggerLog = new BloggerLog();
//            bloggerLog.setLogType(bloggerLogType);
////            bloggerLog.setExtValue("");
//            bloggerLog.setCreateBy(event.getSource());
//            bloggerLog.setUpdateBy(event.getSource());
//        }
//
//        List<SlimBloggerDto> bloggers = bloggerMainRepository.findSlimBloggersScoreAndCount((page-1)*5000, 5000);
//        if (CollectionUtils.isEmpty(bloggers)) {
//            bloggerLog.setCurrentBloggerId(1);
//            bloggerLogRepository.save(bloggerLog);
//            return;
//        }
//
//        bloggerLog.setCurrentBloggerId(page + 1);
//        bloggerLogRepository.save(bloggerLog);
//        ForkJoinPool forkJoinPool = ThreadPoolUtils.forkJoinPool(50, "RefreshAwemeCommentEvent work");
//
//        AtomicInteger atomicInteger = new AtomicInteger(0);
//        try {
//            forkJoinPool.submit(() ->
//                    bloggers.parallelStream().forEach(b -> {
//                        try {
//                            val awemes = awemeService.get30DayBloggerAwemes(b.getUid());
//                        /*synchronized(this){
//                            // redis日志记录
//                            LogUtil.addCountSize(awemes.size());
//                        }*/
//                            atomicInteger.addAndGet(awemes.size());
//                            val saveAwemeCommentEvent = new SaveAwemeCommentEventListener.SaveAwemeCommentEvent(event, awemes);
//                            eventBusTemplate.post(saveAwemeCommentEvent);
//                        } catch (Exception ex) {
//                            log.error("RefreshAwemeCommentEventListener bloggerId:{},exception={}" , b.getUid(), ex);
//                        }
//                    })
//            ).get();
//        }catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }finally{
//            forkJoinPool.shutdown();
//        }
//        XxlJobLogger.log("RefreshAwemeCommentEventListener awemeSize:{}",atomicInteger.get());
//
//        // LogUtil.delLogData();
//
//        /*bloggers.forEach(b ->{
//            val awemes = awemeService.getBloggerAwemes(b.getUid());
//            List<Long> awemeIds = awemes.stream().map(Aweme::getOAwemeId).collect(Collectors.toList());
//            val saveAwemeCommentEvent = new SaveAwemeCommentEventListener.SaveAwemeCommentEvent(event, awemeIds);
//            eventBusTemplate.post(saveAwemeCommentEvent);
//        });*/
//
//
//    }
//
//    public static class RefreshAwemeCommentEvent extends Event {
//
//        public RefreshAwemeCommentEvent() {
//            super("RefreshAwemeCommentEvent");
//        }
//    }
//}
